/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import control.OrderStockList;
import control.CustomerStockList;
import handlers.ProductGroupHandler;
import handlers.StockListHandler;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Customer;
import model.ProductGroup;
import model.Stock;

/**
 *
 * @author Junjie
 */
public class ProgramGUI extends javax.swing.JFrame
{
    private Customer customer;
    private ArrayList<ProductGroup> productGroups;
    private ArrayList<Stock> inStock;

    /**
     * Creates new form ProgramGUI
     */
    public ProgramGUI(Customer cust)
    {
        int width = 1215;
        int height = 680;
        
        this.customer = cust;
        //setSize(width, height);
        setLayout(null);
        setResizable(false);
        setBounds(0, 0, width, height);
        setLocationRelativeTo(null);
        

        try
        {
            productGroups = ProductGroupHandler.getAllProductGroups();
        }
        catch(ClassNotFoundException ex)
        {
        }
        catch(SQLException ex)
        {
        }

        initComponents();

        panelOrder.setVisible(false);
        
        buildStockList(0);
        buildOrderStockList(0);

        jPanel1.setSize(width, height);

        /*
         * Udfylder Kundeinformationerne
         */
        txtBrugerID.setText("" + customer.getUserID());
        txtBrugernavn.setText(customer.getUsername());
        txtAdgangskode.setText(customer.getPassword());

        txtKundeID.setText("" + customer.getCustomerID());
        txtFirmaNavn.setText(customer.getCustomerFirm());
        txtKontaktperson.setText("");

        txtTelefon.setText("");
        txtAdresse.setText("");
        txtPostnr.setText("");

        comboLagerVaregruppe.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                ProductGroup pg = (ProductGroup) comboLagerVaregruppe.getSelectedItem();
                buildStockList(pg.getGroupID());
            }
        });
        
        comboBestilVaregruppe.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                ProductGroup pg = (ProductGroup) comboBestilVaregruppe.getSelectedItem();
                buildOrderStockList(pg.getGroupID());
            }
        });
    }

    public void buildStockList(int gID)
    {
        listLager.removeAll();
        
        try
        {
            inStock = StockListHandler.fetchStock(customer.getCustomerID(), gID);
        }
        catch(ClassNotFoundException ex)
        {
        }
        catch(SQLException ex)
        {
        }

        for(int i = 0; i < inStock.size(); i++)
        {
            CustomerStockList s = new CustomerStockList(inStock.get(i));
            s.setBounds(0, i * 100, 279, 100);
            listLager.add(s);
        }
        
        listLager.setPreferredSize(new Dimension(250, 100 * inStock.size()));
        listLager.revalidate();
        listLager.repaint();
    }

    public void buildOrderStockList(int gID)
    {
        listBestilLager.removeAll();
        
        try
        {
            inStock = StockListHandler.fetchStock(1, gID);
        }
        catch(ClassNotFoundException ex)
        {
        }
        catch(SQLException ex)
        {
        }

        for(int i = 0; i < inStock.size(); i++)
        {
            OrderStockList s = new OrderStockList(inStock.get(i));
            s.setBounds(0, i * 100, 279, 100);
            listBestilLager.add(s);
        }
        
        listBestilLager.setPreferredSize(new Dimension(250, 100 * inStock.size()));
        listBestilLager.revalidate();
        listBestilLager.repaint();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtAdgangskode = new javax.swing.JLabel();
        txtBrugernavn = new javax.swing.JLabel();
        txtBrugerID = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtKundeID = new javax.swing.JLabel();
        txtFirmaNavn = new javax.swing.JLabel();
        txtKontaktperson = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtTelefon = new javax.swing.JLabel();
        txtAdresse = new javax.swing.JLabel();
        txtPostnr = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        panelStock = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listLager = new javax.swing.JList();
        comboLagerVaregruppe = new javax.swing.JComboBox(productGroups.toArray());
        panelOrder = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        comboBestilVaregruppe = new javax.swing.JComboBox(productGroups.toArray());
        jScrollPane3 = new javax.swing.JScrollPane();
        listBestilLager = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        listKurv = new javax.swing.JList();
        jLabel15 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(1215, 680));
        setMinimumSize(new java.awt.Dimension(1215, 680));
        setName("ProgramGUI"); // NOI18N
        setPreferredSize(new java.awt.Dimension(1215, 680));

        jPanel1.setMaximumSize(new java.awt.Dimension(1215, 680));
        jPanel1.setMinimumSize(new java.awt.Dimension(1215, 680));
        jPanel1.setPreferredSize(new java.awt.Dimension(1215, 680));
        jPanel1.setLayout(null);

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(null);

        jLabel1.setText("Kunde oplysninger");
        jPanel2.add(jLabel1);
        jLabel1.setBounds(5, 5, 120, 14);

        jLabel2.setText("Bruger ID:");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(100, 25, 80, 14);

        jLabel3.setText("Brugernavn:");
        jPanel2.add(jLabel3);
        jLabel3.setBounds(100, 40, 80, 14);

        jLabel4.setText("Adgangskode:");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(100, 55, 80, 14);

        txtAdgangskode.setText("-");
        jPanel2.add(txtAdgangskode);
        txtAdgangskode.setBounds(185, 55, 200, 14);

        txtBrugernavn.setText("-");
        jPanel2.add(txtBrugernavn);
        txtBrugernavn.setBounds(185, 40, 200, 14);

        txtBrugerID.setText("-");
        jPanel2.add(txtBrugerID);
        txtBrugerID.setBounds(185, 25, 200, 14);

        jLabel8.setText("Kunde ID:");
        jPanel2.add(jLabel8);
        jLabel8.setBounds(400, 25, 80, 14);

        jLabel9.setText("Firma navn:");
        jPanel2.add(jLabel9);
        jLabel9.setBounds(400, 40, 80, 14);

        jLabel10.setText("Kontakperson:");
        jPanel2.add(jLabel10);
        jLabel10.setBounds(400, 55, 80, 14);

        txtKundeID.setText("-");
        jPanel2.add(txtKundeID);
        txtKundeID.setBounds(485, 25, 200, 14);

        txtFirmaNavn.setText("-");
        jPanel2.add(txtFirmaNavn);
        txtFirmaNavn.setBounds(485, 40, 200, 14);

        txtKontaktperson.setText("-");
        jPanel2.add(txtKontaktperson);
        txtKontaktperson.setBounds(485, 55, 200, 14);

        jLabel5.setText("Telefon:");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(700, 25, 110, 14);

        jLabel6.setText("Adresse:");
        jPanel2.add(jLabel6);
        jLabel6.setBounds(700, 40, 110, 14);

        jLabel7.setText("Postnummer og by:");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(700, 55, 110, 14);

        txtTelefon.setText("-");
        jPanel2.add(txtTelefon);
        txtTelefon.setBounds(815, 25, 200, 14);

        txtAdresse.setText("-");
        jPanel2.add(txtAdresse);
        txtAdresse.setBounds(815, 40, 200, 14);

        txtPostnr.setText("-");
        jPanel2.add(txtPostnr);
        txtPostnr.setBounds(815, 55, 200, 14);

        jButton1.setText("Log ud");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);
        jButton1.setBounds(1100, 10, 70, 30);

        jPanel1.add(jPanel2);
        jPanel2.setBounds(10, 11, 1180, 79);

        panelStock.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel11.setText("Lager");

        jLabel12.setText("Varegruppe:");

        jScrollPane2.setViewportView(listLager);

        javax.swing.GroupLayout panelStockLayout = new javax.swing.GroupLayout(panelStock);
        panelStock.setLayout(panelStockLayout);
        panelStockLayout.setHorizontalGroup(
            panelStockLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelStockLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelStockLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelStockLayout.createSequentialGroup()
                        .addGroup(panelStockLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelStockLayout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboLagerVaregruppe, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        panelStockLayout.setVerticalGroup(
            panelStockLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelStockLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addGap(18, 18, 18)
                .addGroup(panelStockLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(comboLagerVaregruppe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel1.add(panelStock);
        panelStock.setBounds(10, 96, 324, 534);

        panelOrder.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel13.setText("Bestil produkter");

        jLabel14.setText("Varegruppe:");

        jScrollPane3.setViewportView(listBestilLager);

        jScrollPane4.setViewportView(listKurv);

        jLabel15.setText("Indkøbskurv");

        javax.swing.GroupLayout panelOrderLayout = new javax.swing.GroupLayout(panelOrder);
        panelOrder.setLayout(panelOrderLayout);
        panelOrderLayout.setHorizontalGroup(
            panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelOrderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addGroup(panelOrderLayout.createSequentialGroup()
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboBestilVaregruppe, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 93, Short.MAX_VALUE)
                .addGroup(panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addContainerGap())
        );
        panelOrderLayout.setVerticalGroup(
            panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelOrderLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13)
                .addGap(18, 18, 18)
                .addGroup(panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(comboBestilVaregruppe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3)
                    .addGroup(panelOrderLayout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 169, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel1.add(panelOrder);
        panelOrder.setBounds(473, 96, 717, 534);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        LoginGUI login = new LoginGUI();
        login.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(ProgramGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(ProgramGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(ProgramGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(ProgramGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new ProgramGUI().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboBestilVaregruppe;
    private javax.swing.JComboBox comboLagerVaregruppe;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JList listBestilLager;
    private javax.swing.JList listKurv;
    private javax.swing.JList listLager;
    private javax.swing.JPanel panelOrder;
    private javax.swing.JPanel panelStock;
    private javax.swing.JLabel txtAdgangskode;
    private javax.swing.JLabel txtAdresse;
    private javax.swing.JLabel txtBrugerID;
    private javax.swing.JLabel txtBrugernavn;
    private javax.swing.JLabel txtFirmaNavn;
    private javax.swing.JLabel txtKontaktperson;
    private javax.swing.JLabel txtKundeID;
    private javax.swing.JLabel txtPostnr;
    private javax.swing.JLabel txtTelefon;
    // End of variables declaration//GEN-END:variables
}
